package cn.xwt.weidisk.controller;

import cn.xwt.weidisk.constant.ErrorCode;
import cn.xwt.weidisk.pojo.dao.LoginLogDo;
import cn.xwt.weidisk.pojo.dto.*;
import cn.xwt.weidisk.service.UserService;
import cn.xwt.weidisk.util.CommonUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 薛慰涛
 * 用户账号操作
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    /**
     * 查询用户信息
     * @return userData
     */
    @GetMapping("/info")
    public UserData getUserInfo() {
        return userService.selectUser(CommonUtils.getUid());
    }

    /**
     * 保存头像
     * @param map 头像base64
     * @return Resp
     */
    @PostMapping("/save-avatar")
    public Resp saveAvatar(@RequestBody Map<String, String> map) {
        String avatar = map.get("avatar");
        if (userService.saveAvatar(avatar, CommonUtils.getUid())) {
            return Resp.success();
        } else {
            return Resp.serverError();
        }
    }

    /**
     * 获取图像链接
     * @return String
     */
    @GetMapping("/get-avatar-link")
    public String getAvatarLink() {
        return userService.getAvatarLink(CommonUtils.getUid());
    }

    /**
     * 修改用户名
     * @param usernameData 用户名信息
     * @return resp
     */
    @PutMapping("/update-username")
    public Resp updateUsername(@RequestBody UsernameData usernameData) {
        usernameData.setId(CommonUtils.getUid());
        switch (userService.updateUsername(usernameData)) {
            case CODE_ERROR:
                return new Resp(ErrorCode.INCORRECT_VERIFICATION_CODE.getCode(), "验证码错误");
            case USERNAME_EXISTS:
                return new Resp(ErrorCode.USERNAME_ALREADY_EXISTS.getCode(), "用户名已存在");
            default:
                return Resp.success();
        }
    }

    /**
     * 修改密码
     * @param passwordData 用户名信息
     * @return resp
     */
    @PutMapping("/update-password")
    public Resp updatePassword(@RequestBody PasswordData passwordData) {
        passwordData.setId(CommonUtils.getUid());
        if (userService.updatePassword(passwordData)) {
            return Resp.success();
        } else {
            return new Resp(ErrorCode.INCORRECT_VERIFICATION_CODE.getCode(), "验证码错误");
        }
    }

    /**
     * 修改邮箱
     * @param emailData 邮箱信息
     * @return resp
     */
    @PutMapping("/update-email")
    public Resp updateEmail(@RequestBody EmailData emailData) {
        emailData.setId(CommonUtils.getUid());
        switch (userService.updateEmail(emailData)) {
            case CODE_ERROR:
                return new Resp(ErrorCode.INCORRECT_VERIFICATION_CODE.getCode(), "验证码错误");
            case PASSWORD_ERROR:
                return new Resp(ErrorCode.PASSWORD_VERIFICATION_FAILED.getCode(), "密码错误");
            default:
                return Resp.success();
        }
    }

    /**
     * 保存登录日志
     * @param type 设备类型
     */
    @PostMapping("/login-log")
    public void saveLoginLog(@RequestParam("type") String type) {
        userService.saveLoginLog(CommonUtils.getUid(), type, CommonUtils.getIp());
    }

    @GetMapping("/login-log")
    public List<LoginLogDo> selectLoginLogList() {
        return userService.selectLoginLogList(CommonUtils.getUid());
    }
}
